/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/

#ifndef SCHM_MSGBX_H_
#define SCHM_MSGBX_H_



#define SchM_Enter_Msgbx_MSGBX_EXCLUSIVE_AREA_00()  SuspendOSInterrupts()
#define SchM_Enter_Msgbx_MSGBX_EXCLUSIVE_AREA_01()  SuspendOSInterrupts()


#define SchM_Exit_Msgbx_MSGBX_EXCLUSIVE_AREA_00()   ResumeOSInterrupts()
#define SchM_Exit_Msgbx_MSGBX_EXCLUSIVE_AREA_01()   ResumeOSInterrupts()
#define OSEE_STATIC_INLINE  static inline
typedef uint32_t              OsEE_reg;
#if (!defined(OSEE_STRING))
#define OSEE_STRING(stringtoquote) #stringtoquote
#endif /* !OSEE_STRING */


#if (!defined(OSEE_S))
#define OSEE_S(m)             OSEE_STRING(m)
#endif /* !OSEE_S */

#define OSEE_ARMV7_MRS(var,reg)   \
  __asm__ volatile("mrs %0, " OSEE_S(reg) "\n\t" : "=&r"(var) : : "memory")



/* Disable/Enable Interrupts */
OSEE_STATIC_INLINE void disableIRQ( void )
{
  __asm__ volatile("cpsid i\n\t");
}

OSEE_STATIC_INLINE void enableIRQ( void )
{
  __asm__ volatile("cpsie i\n\t");
}

/* Suspend/Resume Interrupts */
OSEE_STATIC_INLINE OsEE_reg suspendIRQ(void)
{
  OsEE_reg flags;
  OSEE_ARMV7_MRS(flags,CPSR);
  disableIRQ();
  return flags;
}

OSEE_STATIC_INLINE void resumeIRQ(OsEE_reg flags)
{
    enableIRQ();
}

void SuspendOSInterrupts() {
    suspendIRQ();
}
void ResumeOSInterrupts() {
    resumeIRQ(0);
}
#endif
